<template>
	<view>
		<swiper @change="__onSwiperChange" :style="[{height:height + 'px'}]" circular>
			<swiper-item v-for="(item,i) in pages" :key="i">
				<view class="hotkey" :style="gridStyle">
					<view v-for="(hotkey,j) in item" :key="j" class="hotkey-item" @click="__onHotkeyTap"
						:data-data="hotkey">
						<image class="icon" mode="aspectFill" :src="hotkey.imageUrl"></image>
						<view class="name">{{ hotkey.title }}</view>
					</view>
				</view>
			</swiper-item>
		</swiper>
		<!-- 进度条 -->
		<view class="dot" v-if="pages.length>1">
			<block v-for="(item,index) in pages" :key="index">
				<view :class="current==index?'dot-item active':'dot-item'"></view>
			</block>
		</view>
	</view>
</template>

<script>
	export default {
		name: "kong-kim",
		props: {
			value: {
				type: Array,
				value: [],
			},
			row: { // 行
				type: Number,
				value: 1,
			},
			column: { // 列
				type: Number,
				value: 6,
			},
		},
		watch: {
			value(newValue) {
				this.pages = newValue
				this.$nextTick(() => {
					this.__viewInit()
				})
			},
			row(newValue) {
				this.row = newValue
				this.$nextTick(() => {
					this.__viewInit()
				})
			},
			column(newValue) {
				this.column = newValue
				this.$nextTick(() => {
					this.__viewInit()
				})
			}
		},
		data() {
			return {
				height: 0,
				current: 0,
				pages: [],
			};
		},
		computed: {
			gridStyle() {
				var column = this.column;
				var row = this.row;
				var style = `grid-template-rows: repeat(${row}, 120rpx);`
				if (column >= 3) {
					style = `${style}grid-template-columns:repeat(${column}, 1fr);`
				} else {
					style = `${style}grid-template-columns:repeat(${column}, 100rpx);`
				}
				return style
			},
		},
		methods: {
			__viewInit() {
				let data = this.value
				let len = this.row * this.column
				var arr = []
				for (var i = 0; i < data.length; i += len) {
					arr.push(data.slice(i, i + len))
				}
				this.pages = arr
				//自适应高度
				const query = uni.createSelectorQuery().in(this);
				query.select('.hotkey').boundingClientRect(res => {
					if (!res) {
						return
					}
					this.height = res.height
				}).exec();
			},
			__onSwiperChange(e) {
				//console.log("__onSwiperChange", e)
				this.current = e.detail.current;
			},
			__onHotkeyTap(e) {
				//自定义组件事件 onKongKim
				var data = e.currentTarget.dataset.data
				this.$emit('onKongKim', data.jumpUrl)
			}
		}
	}
</script>

<style lang="scss">
	.hotkey {
		display: grid;
		grid-auto-flow: column;
		grid-gap: 10rpx;

		&-item {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.icon {
				width: 74rpx;
				height: 74rpx;
			}

			.name {
				margin-top: 9rpx;
				font-size: 24rpx;
				// color: #C28237;
				color: #977159;
			}
		}
	}

	.dot {
		width: 100%;
		padding: 15rpx 0;
		display: flex;
		justify-content: center;
		align-items: center;

		&-item {
			width: 40rpx;
			height: 10rpx;
			border-radius: 10rpx;
			background-color: #eee;
		}

		&-item.active {
			width: 40rpx;
			background-color: darkorange;
		}
	}
</style>
